#ifndef SHARED_PTR_H
#define SHARED_PTR_H

#include <iostream>
/**
 * 智能指针实现：引用计数器
*/
template <typename T>
class SharedPtr {
private:
    T* ptr_;
    size_t counter_;
public:
    SharedPtr(T* ptr) : ptr_(ptr), counter_(1) {
    }

    SharedPtr(const SharedPtr& other) {
        counter_ = other.counter_ + 1;
        ptr_ = other.ptr_;
    }

    SharedPtr& operator=(const SharedPtr& right) {
        if (--counter_ == 0) {
            delete ptr_;
        }
        counter_ = right.counter_ + 1;
        ptr_ = right.ptr_;
        return *this;
    }

    ~SharedPtr() {
        if (--counter_ == 0) {
            std::cout << "delete ptr:" << *ptr_ << std::endl;
            delete ptr_;
        }
    }
/**
 * 下面是辅助函数，核心是上面的几个函数
*/
    size_t use_count() {
        return counter_;
    }

    T& operator *() {        //重载*操作符
        return *ptr_;
    }

    T* operator ->() {       //重载->操作符
        return ptr_;
    }
};

#endif